package com.lemon.smartsafety.ui.fragment.bind;

import android.os.Bundle;
import android.view.View;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.bind.GovInfoVo;
import com.lemon.smartsafety.entity.event.bind.BindGovItemEvent;
import com.lemon.smartsafety.entity.event.bind.BindNewGovEvent;
import com.lemon.smartsafety.http.service.BindInfoServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.adapter.bind.BindGovAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import butterknife.OnClick;
import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/4/8.
 */
public class BindGovListFragment extends BasePullToRefreshFragment<GovInfoVo, List<GovInfoVo>> implements RecyclerTClickListener<GovInfoVo> {
    private boolean mCanNew          = false;
    private String  mKeyWord         = "";
    private String  mApplicationCode = "";

    public static BindGovListFragment newInstance(String applicationCode, String text, boolean canNew) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.APPLICATION_CODE, StringUtils.nullValue(applicationCode));
        bundle.putString(BundleKey.KEY_WORD, StringUtils.nullValue(text));
        bundle.putBoolean(BundleKey.CAN_NEW, canNew);
        BindGovListFragment pageFragment = new BindGovListFragment();
        pageFragment.setArguments(bundle);
        return pageFragment;
    }


    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        if (getArguments() != null) {
            mApplicationCode = getArguments().getString(BundleKey.APPLICATION_CODE);
            mKeyWord = getArguments().getString(BundleKey.KEY_WORD);
            mCanNew = getArguments().getBoolean(BundleKey.CAN_NEW, false);
        }
        mKeyWord = StringUtils.nullValue(mKeyWord);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }


    @Override
    protected void initViews() {
        super.initViews();
        pageSize = 40;
        fab.setVisibility(View.VISIBLE);
        /*
        if (mCanNew) {
            fab.setVisibility(View.VISIBLE);
        } else {
            fab.setVisibility(View.GONE);
        }
        */


    }


    @Override
    public LemonRcvAdapter<GovInfoVo> getLemonAdapter() {
        return new BindGovAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return EventTag.TAG_GOV_LIST;
    }

    @Override
    public void onLoadMore() {
        if (mCanNew) {//这个标识是 是否限制的意思，懒得修改了。郁闷
            BindInfoServices.init().getAllGov(RefreshType.LOAD_MORE, pageSize, currentPage, mKeyWord);
        } else {
            BindInfoServices.init()
                            .getScopeGov(RefreshType.LOAD_MORE, pageSize, currentPage, mApplicationCode, mKeyWord);
        }

    }

    @Override
    public void onPullRefresh() {
        if (mCanNew) {
            BindInfoServices.init().getAllGov(RefreshType.REFRESH, pageSize, currentPage, mKeyWord);
        } else {
            BindInfoServices.init().getScopeGov(RefreshType.REFRESH, pageSize, currentPage, mApplicationCode, mKeyWord);
        }
    }


    @Override
    public void onItemClick(final GovInfoVo data, int position) {
        BindGovItemEvent event = new BindGovItemEvent();
        event.setPosition(position);
        event.setData(data);
        event.setTag(BindGovListFragment.class.getName());
        AppService.getBus().post(event);
    }


    @OnClick({R.id.float_button})
    public void fabClick(View view) {
        BindNewGovEvent event = new BindNewGovEvent();
        event.setTag(BindGovListFragment.class.getName());
        AppService.getBus().post(event);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        OkHttpUtils.getInstance().cancelTag(getHttpTag());
    }
}
